﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Collections;

namespace JLM2
{
    public class JLMCommTaskRunService
    {
        protected JLMDataExchange dataExchange;
        protected Thread serviceThread = null;
        protected JLMComm jlmComm = null;
        protected bool endThreadFlag = false;
        protected bool threadRunningFlag = false;
        protected string testText = "";
        protected bool readAll = false;
        public JLMCommTaskRunService(JLMDataExchange exchange)
        {
            dataExchange = exchange;
        }

        public void Start(JLMComm comm)
        {
            endThreadFlag = false;
            serviceThread = new Thread(new ThreadStart(this.ServiceMain));
            jlmComm = comm;

            //启动线程
            serviceThread.Start();
        }

        public void Stop()
        {
            endThreadFlag = true;
            while(threadRunningFlag)
            {
                Thread.Sleep(1);
            }
        }

        public void SetText(string text)
        {
            testText = text;
        }

        public void SetReadAll(bool readllFlag)
        {
            readAll = readllFlag;
        }
        public void ServiceMain()
        {
            threadRunningFlag = true;
            while(!endThreadFlag)
            {
                // 执行各类任务
                //dataExchange.BH = testText;
                if(readAll)
                {
                    jlmComm.ReadJLMData(dataExchange);
                }else
                {
                    jlmComm.ReadRunStatus(dataExchange);
                }
                if(endThreadFlag)
                {
                    break;
                }
                Thread.Sleep(1000);
            }

            threadRunningFlag = false;
        }
    }
}
